<?php
/**
 * fly
 * Copyright By 5920Team (http://www.5920.me)
 * User: yu@5920.me
 * Date: 13-9-27
 * Time: 上午1:54
 */


class Router
{

    /**
     * @var Module
     */
    static private $module;

    /**
     * @var array
     */
    static private $conf;

    /**
     * @param $config
     */
    static public function run($config)
    {
        self::$conf = $config;

        switch (self::$conf['method'])
        {
            case 1:
                $module  = empty($_GET['m']) ? self::$conf['default_module'] : $_GET['m'];
                $action  = empty($_GET['a']) ? self::$conf['default_action'] : $_GET['a'];
                break;
            default:
                exit('error router method.');
                break;
        }
        self::work($module, $action);
    }

    /**
     * @param $module
     * @param $action
     */
    static private function work($module, $action)
    {
        $module .= self::$conf['module_postfix'];
        $moduleFileName = FLY_MODULE_PATH . '/' . $module . 'php';
        if(!is_file($moduleFileName))
        {
            $module = self::$conf['default_module'] . self::$conf['module_postfix'];
        }
        self::$module = new $module;
        /** action whileList */
        if (!in_array($action, self::$module->actionList))
        {
            $action = self::$conf['default_action'];
        }
        /** Method beginList*/
        if(isset(self::$module->beginMethodList[$action]))
        {
            foreach (self::$module->beginMethodList[$action] as $method)
            {
                self::$module->$method();
            }
        }
        self::$module->$action();
    }

}